<?php

class ofPropelPeer extends BasePeer
{
	
	/**
	 * 区切り文字
	 *
	 */
	const SEP = "___";
	
	/**
	 * 連想配列経由で結果を返す
	 *
	 * @param Criteria $c
	 * @param Connection $con
	 * 
	 * @return Array オブジェクト
	 */
	public static function doSelectAO(Criteria $c, $con = null)
	{
		
		$rs = self::doSelectAssoc($c, $con);
		
		return self::parseAssocToObjects($rs);
		
	}
	
	/**
	 * 連想配列で取得
	 *
	 * @param Criteria $c
	 * @param Connection $con
	 * @return PDOStatement
	 */
	public static function doSelectAssoc(Criteria $c, $con = null)
	{
		
		$selectColumn = $c->getAsColumns();
		
		foreach($c->getSelectColumns() as $column)
		{
			
			$alias = str_replace(".", self::SEP, $column);
			
			$selectColumn[$alias] = $column;
		}
		
		//$c->clearSelectColumns();
		
		foreach($selectColumn as $alias => $column)
		{
			
			$c->addAsColumn($alias, $column);
			
		}
		
		$rs = BasePeer::doSelect($c, $con);
		
		return $rs;
	}
	
	
	
	
	
	/**
	 * 
	 *
	 * @param PDOStatement $stmt
	 * @return Array
	 */
	public static function parseAssocToObjects(PDOStatement  $stmt)
	{
		
		while($rs = $stmt->fetchObject(ofPropelRS::CLASS_NAME)){
			/* @var $rs ofPropelRS */
			
			$varObject = array();
			foreach($rs->getRow() as $name => $value)
			{
				$n = explode(self::SEP, $name);
				
				if(count($n) < 2){
					$varObject['__column_' . $name] = $value;
					continue;
				}
				$d = Propel::getDatabaseMap('propel');
				
				if($d->getTable($n[0]) instanceof TableMap ){
					$object = $d->getTable($n[0])->getPhpName();
					
					if(!@$varObject[$object] instanceof $object){
						
						$varObject[$object] = new $object;
					}
					
					if($d->getTable($n[0])->getColumn($n[1]) instanceof ColumnMap ){
						$method = 'set'.$d->getTable($n[0])->getColumn($n[1])->getPhpName();
						try{
							$varObject[$object]->$method($value);
						}catch (PropelException $e){
							
						}
					}
					
				}
				
			}
			
			$result[]=$varObject;
			
			
		}
		
		return $result;
		
	}
	
	/**
	 * ResultSet内のデータをオブジェクトにセット
	 *
	 * @param ResultSet $rs
	 */
  public static function parseAssocToMO(ofPropelRS $rs)
	{
		$arg = func_get_args();
		unset($arg[0]);
		foreach($arg as $a){
			
			self::parseAssocToObject($rs, $a);
			
		}
		
	}
	
	
	public static function parseAssocToObject(ofPropelRS $rs, $object)
	{
		
		if($object instanceof BaseObject)
		{
		  $tablename = $object->getPeer()->getTableMap()->getName();
		}else{
			
			list($tablename, $ob) = $object;
			$object = $ob;
			
		}
		foreach($rs->getRow() as $name => $value)
		{
			$n = explode(self::SEP, $name);
			
			
			if($n[0] !== $tablename or count($n) < 2 ){
				
				continue;
			}
			$m = $object->getPeer()->getTableMap()->getColumn($n[1])->getPhpName();
			
			if(method_exists($object, $method = "set" . $m)){
				try{
					$object->$method($value);
				}catch (PropelException $e){
					
				}
			}
			
		}
		
		return $object;
		
	}
	
	/**
	 * オブジェクトを配列で
	 *
	 * @param object $object
	 * @param int $id 
	 * @param string $key
	 */
	public static function doSelectByKey($object, $id, $key = "getId")
	{
		
		static $a;
		$className = get_class($object);
		if(!isset($a[$className])){
			
			
			$rs = $object->doSelect(new Criteria());
			
			foreach($rs as $r){
				
				$a[$className][$r->$key()] = $r;
				
			}
			
		}
		
		if(!isset($a[$className][$id])){
			
			$ob = str_replace("Peer", "",$className);
			
			return new $ob;
			
		}
		
		return $a[$className][$id];
	}
	
	/**
	 * 
	 * 
	 * @param Object $peer 
	 * @param Criteria $c
	 * @param string $a
	 */
	public static function addSelectColumnsByAlias($peer, Criteria $c, $a)
	{
		
		$f = $peer->getFieldNames(self::TYPE_FIELDNAME);
		
		foreach($f as $name)
		{
			$name = strtoupper($name);
			
			$c->addAsColumn(
			 $a . self::SEP . $name, 
			 $a.".".$name);
		}
		
	}
	
	
	
	
}